package com.n5x.controller.admin.promotion;

import com.github.pagehelper.PageHelper;
import com.n5x.common.base.JsonModel;
import com.n5x.common.base.ResponseCode;
import com.n5x.controller.BaseController;
import com.n5x.model.goods.*;
import com.n5x.model.promotion.Promotion;
import com.n5x.model.shopping.ShoppingOrder;
import com.n5x.service.IGoodsService;
import com.n5x.service.IPromotionService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 商品接口Controller
 * 需要后台管理员token
 */
@Controller("adminPromotion")
@RequestMapping("admin/promotion")
public class PromotionController extends BaseController {

    @Autowired
    IPromotionService iPromotionService ;

    @RequestMapping(value = "/save")
    @ResponseBody
    public JsonModel save(@RequestParam(value = "token", required = false) String token, HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            String title = request.getParameter("title");
            String picture = request.getParameter("picture");
            String content =request.getParameter("content");
            String goodsId = request.getParameter("goodsId");
            String isTop = request.getParameter("isTop");
            String disable = request.getParameter("disable");
            if(StringUtils.isBlank(title)){
                return getResultError(ResponseCode.Common_ParamError,"活动标题不能为空");
            }
            if(StringUtils.isBlank(content)){
                return getResultError(ResponseCode.Common_ParamError,"活动内容不能为空");
            }
            if(StringUtils.isBlank(picture)){
                return getResultError(ResponseCode.Common_ParamError,"活动图片不能为空");
            }
            if(StringUtils.isBlank(goodsId)){
                return getResultError(ResponseCode.Common_ParamError,"活动物品ID不能为空");
            }
            if(StringUtils.isBlank(isTop)){
                return getResultError(ResponseCode.Common_ParamError,"活动置顶不能为空");
            }
            if(StringUtils.isBlank(disable)){
                return getResultError(ResponseCode.Common_ParamError,"是否活动中不能为空");
            }
            Promotion promotion = null ;
            if(StringUtils.isBlank(id)){
                promotion = new Promotion();
            }else{
                promotion =iPromotionService.get(id);
            }
            promotion.setTitle(title);
            promotion.setPicture(picture);
            promotion.setContent(content);
            promotion.setGoodsId(goodsId);
            promotion.setTop(isTop.equals("1"));
            promotion.setDisable(disable.equals("1"));
            logger.info("[保存更新Promotion]:",new Object[]{promotion});
            iPromotionService.save(promotion);
            return getResult(promotion.getId());
        } catch (Exception e) {
            logger.error("[保存更新Promotion失败]e:{}", new Object[]{e.toString()});
            e.printStackTrace();
            return getResultError(ResponseCode.Common_Error, e.toString());
        }
    }
}